<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/10
 * Time: 13:56
 */

namespace app\admin\controller;

use think\Db;

class Privatebuyorder extends Base
{
    /**
     * 私人订制套餐购买订单列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = trim(input('keyword'), ' '); // 订单编号
            $user_name = trim(input('user_name'), ' '); // 用户姓名
            $order_status = input('order_status'); // 1 未支付  2 已支付  3 已取消
            $order_type = input('order_type'); // 1 加盟商   2 普通用户
            $pay_type = input('pay_type'); // 1 微信   2 支付宝   3 积分

            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['order_no'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($user_name) && $user_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($order_status) && $order_status !=''){
                $where['order_status'] = $order_status;
            }
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            if(isset($pay_type) && $pay_type != ''){
                $where['order_pay_type'] = $pay_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;

            $count = Db::table('up_private_buy_order')
                ->join('up_u_user', 'order_user_id = user_id', 'LEFT')
                ->join('up_private_setmeal', 'order_ps_id = ps_id', 'LEFT')
                ->where($where)
                ->count('order_id');
            $lists = Db::table('up_private_buy_order')
                ->join('up_u_user', 'order_user_id = user_id', 'LEFT')
                ->join('up_private_setmeal', 'order_ps_id = ps_id', 'LEFT')
                ->field('order_id, order_no, user_real_name, ps_name, order_pay_type, order_number, order_total_price, 
                order_type, order_status, order_time')
                ->where($where)
                ->order('order_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 取消订单
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function cancelOrder(){
        $order_id = input('id');
        $order = Db::table('up_private_buy_order')->where('order_id', $order_id)->field('order_status')->find();
        if($order['order_status'] != 1) return json(['code' => 100, 'msg' => '订单状态异常']);

        $res = Db::table('up_private_buy_order')
            ->where('order_id', $order_id)
            ->update([
                'order_status' => 3,
                'order_cancel_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 导出订单
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $order_ids = input('ids');

        $where = [];
        if($order_ids == -1){
            $keyword = trim(input('keyword'), ' '); // 订单编号
            $user_name = trim(input('user_name'), ' '); // 用户姓名
            $order_status = input('order_status'); // 1 未支付  2 已支付  3 已取消
            $order_type = input('order_type'); // 1 加盟商   2 普通用户
            $pay_type = input('pay_type'); // 1 微信   2 支付宝   3 积分

            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['order_no'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($user_name) && $user_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($order_status) && $order_status !=''){
                $where['order_status'] = $order_status;
            }
            if(isset($order_type) && $order_type != ''){
                $where['order_type'] = $order_type;
            }
            if(isset($pay_type) && $pay_type != ''){
                $where['order_pay_type'] = $pay_type;
            }
        }else{
            if($order_ids == ''){
                return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            }
            $where['order_id'] = ['IN', $order_ids];
        }

        $order = Db::table('up_private_buy_order')
            ->join('up_u_user', 'order_user_id = user_id', 'LEFT')
            ->join('up_private_setmeal', 'order_ps_id = ps_id', 'LEFT')
            ->field('order_id, order_no, user_real_name, ps_name, order_pay_type, order_number, order_total_price, 
                order_type, order_status, order_time, user_phone')
            ->where($where)
            ->order('order_time desc')
            ->select();
        foreach($order as $k => $v){
            // 订单编号
            $order[$k]['order_no'] = "\t" . $v['order_no'] . "\t";
            // 手机号
            $order[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            // 支付方式
            $order[$k]['order_pay_type'] = $v['order_pay_type'] == 1 ? '微信' : ($v['order_pay_type'] == 2 ? '支付宝' : '积分');
            // 订单类型
            $order[$k]['order_type'] = $v['order_type'] == 1 ? '加盟商' : '普通用户';
            // 订单状态
            $order[$k]['order_status'] = $v['order_status'] == 1 ? '未支付' : ($v['order_status'] == 2 ? '已支付' : '已取消');
        }
        $cellname = [
            ['order_id', '订单ID', 10, 'LEFT'],
            ['order_no', '订单编号', 20, 'LEFT'],
            ['ps_name', '所属套餐', 20, 'LEFT'],
            ['order_number', '订单数量', 20, 'LEFT'],
            ['order_total_price', '订单总价', 20, 'LEFT'],
            ['user_real_name', '用户姓名', 20, 'LEFT'],
            ['user_phone', '手机号', 20, 'LEFT'],
            ['order_status', '订单状态', 15, 'LEFT'],
            ['order_type', '订单类型', 15, 'LEFT'],
            ['order_pay_type', '支付方式', 15, 'LEFT'],
            ['order_time', '下单时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('私人订制购买订单', 'privateBuyOrder', $cellname, $order);
        return json($res);
    }
}